﻿using System;
using System.Management;
using ClusterAwareProperties;

namespace ClusterAwareProperties
{
    /// <summary>
    /// Convert a Resource ManagementObject to Resource Class
    /// </summary>
    public static class ResourceTranslate
    {
        /// <summary>
        /// Convert Resource ManagementObject to Resource Class
        /// </summary>
        /// <param name="clsres">Resource class</param>
        /// <param name="queryObj">ManagementObject Group</param>
        /// <returns>Resource class with properties converted</returns>
        public static ResourceProperty Convert(this ResourceProperty clsres, ManagementObject queryObj)
        {
            clsres.Caption = queryObj.PropertyValue<String>("Caption");
            clsres.Characteristics = queryObj.PropertyEnum<UInt32, CharacteristicsResource>("Characteristics", CharacteristicsResource.Unknown);
            clsres.CoreResource = queryObj.PropertyValue<bool>("CoreResource");
            clsres.CryptoCheckpoints = queryObj.PropertyValue<string[]>("CryptoCheckpoints");
            clsres.DeadlockTimeout = queryObj.PropertyValue<UInt32?>("DeadlockTimeout");
            clsres.DebugPrefix = queryObj.PropertyValue<string>("DebugPrefix");
            clsres.DeleteRequiresAllNodes = queryObj.PropertyValue<bool>("DeleteRequiresAllNodes");
            clsres.Description = queryObj.PropertyValue<string>("Description");
            clsres.Flags = queryObj.PropertyValue<UInt32>("Flags");
            clsres.InstallDate = queryObj.PropertyValue<DateTime?>("InstallDate");
            clsres.InternalState = queryObj.PropertyValue<string>("InternalState");
            clsres.IsAlivePollInterval = queryObj.PropertyValue<UInt32>("IsAlivePollInterval");
            clsres.IsClusterSharedVolume = queryObj.PropertyValue<bool?>("IsClusterSharedVolume");
            clsres.LocalQuorumCapable = queryObj.PropertyValue<bool>("LocalQuorumCapable");
            clsres.LooksAlivePollInterval = queryObj.PropertyValue<UInt32>("LooksAlivePollInterval");
            clsres.MonitorProcessId = queryObj.PropertyValue<UInt32?>("MonitorProcessId");
            clsres.Name = queryObj.PropertyValue<string>("Name");
            clsres.PendingTimeout = queryObj.PropertyValue<UInt32?>("PendingTimeout");
            clsres.PersistentState = queryObj.PropertyValue<bool>("PersistentState");
            clsres.PrivateProperties = new PrivatePropertiesData(queryObj);
            clsres.QuorumCapable = queryObj.PropertyValue<bool>("QuorumCapable");
            clsres.RegistryCheckpoints = queryObj.PropertyValue<string[]>("RegistryCheckpoints");
            clsres.ResourceClass = queryObj.PropertyValue<UInt32>("ResourceClass");
            clsres.ResourceSpecificStatus = queryObj.PropertyValue<string>("ResourceSpecificStatus");
            clsres.RestartAction = queryObj.PropertyEnum<UInt32, RestartAct>("RestartAction", RestartAct.StateUnknown);
            clsres.RestartDelay = queryObj.PropertyValue<UInt32?>("RestartDelay");
            clsres.RestartPeriod = queryObj.PropertyValue<UInt32>("RestartPeriod");
            clsres.RestartThreshold = queryObj.PropertyValue<UInt32>("RestartThreshold");
            clsres.RetryPeriodOnFailure = queryObj.PropertyValue<UInt32>("RetryPeriodOnFailure");
            clsres.SeparateMonitor = queryObj.PropertyValue<bool>("SeparateMonitor");
            clsres.State = queryObj.PropertyEnum<UInt32, StateResources>("State", StateResources.Unknown);
            clsres.Status = queryObj.PropertyEnum<string, StatusResource>("Status", StatusResource.Unknown);
            clsres.Subclass = queryObj.PropertyValue<UInt32>("Subclass");
            clsres.Type = queryObj.PropertyValue<string>("Type");
            return clsres;
        }

        ///<summary>
        /// Convert Resource class to ManagementObject and Save on Cluster
        ///</summary>
        ///<param name="clsres">Resource class to converter</param>
        ///<param name="opSystem">Operation System where cluster is running</param>
        public static void SaveManagementObject(this ResourceProperty clsres, WindowsOperationSystem opSystem)
        {
            clsres.Instance["Characteristics"] = (UInt32)clsres.Characteristics;
            clsres.Instance["CoreResource"] = clsres.CoreResource;
            if (opSystem == WindowsOperationSystem.Windows2008 || opSystem == WindowsOperationSystem.Windows2008R2)
            {
                clsres.Instance["DeadlockTimeout"] = clsres.DeadlockTimeout;
            }
            if (opSystem == WindowsOperationSystem.Windows2003 || opSystem == WindowsOperationSystem.Windows2003R2)
            {
                clsres.Instance["DebugPrefix"] = clsres.DebugPrefix;
            }
            clsres.Instance["DeleteRequiresAllNodes"] = clsres.DeleteRequiresAllNodes;
            clsres.Instance["Description"] = clsres.Description;
            clsres.Instance["Flags"] = clsres.Flags;
            clsres.Instance["IsAlivePollInterval"] = clsres.IsAlivePollInterval;
            clsres.Instance["LocalQuorumCapable"] = clsres.LocalQuorumCapable;
            clsres.Instance["LooksAlivePollInterval"] = clsres.LooksAlivePollInterval;
            if (opSystem == WindowsOperationSystem.Windows2008 || opSystem == WindowsOperationSystem.Windows2008R2)
            {
                clsres.Instance["MonitorProcessId"] = clsres.MonitorProcessId;
            }
            if (opSystem == WindowsOperationSystem.Windows2008 || opSystem == WindowsOperationSystem.Windows2008R2)
            {
                clsres.Instance["PendingTimeout"] = clsres.PendingTimeout;
            }
            clsres.Instance["PersistentState"] = clsres.PersistentState;
            clsres.Instance["PrivateProperties"] = clsres.PrivateProperties.ConvertToManagemenBasetObject();
            clsres.Instance["QuorumCapable"] = clsres.QuorumCapable;
            clsres.Instance["ResourceClass"] = clsres.ResourceClass;
            clsres.Instance["RestartAction"] = (UInt32)clsres.RestartAction;
            clsres.Instance["RestartPeriod"] = clsres.RestartPeriod;
            clsres.Instance["RestartThreshold"] = clsres.RestartThreshold;
            clsres.Instance["RetryPeriodOnFailure"] = clsres.RetryPeriodOnFailure;
            clsres.Instance["SeparateMonitor"] = clsres.SeparateMonitor;
            clsres.Instance["Subclass"] = clsres.Subclass;
            clsres.Instance["Type"] = clsres.WindowsType.ConvertToResourcetypeName(clsres.Type);
            clsres.Instance.Put();
        }
    }

}
